feat(query-db): support eager initial data#1683
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughQuery Collections now support eager ChangesEager initial data support
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant QueryCollection
participant QueryObserver
participant QueryClient
QueryCollection->>QueryObserver: configure initialData and select
QueryObserver->>QueryClient: read or seed query cache
QueryClient-->>QueryObserver: response and freshness state
QueryObserver-->>QueryCollection: materialized projected rows
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: 0 B Total Size: 125 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 4.22 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 125 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 4.22 kB ℹ️ View Unchanged
|
|
@KyleAMathews i had Fable review the PR and chatted through it with Fable. This is what we landed on: Verdict: approve the architecture; a few things worth addressing before merge — one real risk (persistence interplay), one changeset omission, and some test gaps relative to the PR's own design doc. CI is fully green (unit, E2E, example build, lint/autofix). Issue 1 — persistence interplay can clobber newer persisted rows (main risk)On restart with SQLite persistence, the Query cache is empty but persisted rows exist under
So a retention mechanism meaning "keep these rows until the server revalidates them" is now satisfiable by static config data with no network involved, silently regressing the user's last-seen state. Mechanically it's "same as a stale server response," but there's no server in the loop. Suggestions:
Issue 2 — changeset omits two silent behavior changesThe changeset only advertises the new feature, but the PR also changes existing behavior for collections that never opt in:
Both are deliberate correctness fixes, but they remove rows an app may currently be (mis)relying on. They should be called out in the changeset so the release notes warn anyone affected. Issue 3 — test coverage vs. the PR's own claimsCovered well: fresh/stale seeds, function-initializer-evaluated-once, wrapped envelope preserved in cache, shared-client scoping, existing-cache precedence, on-demand rejection and default suppression, placeholder suppression. Missing relative to the design doc's behavior table and its own implementation sequence (steps 3–5):
Given that, "Closes #346" is a stretch by the design doc's own sequencing ("close or narrow #346 only after these contracts ship" — step 6). Suggest "Advances #346" unless the error-retention and persistence tests land in this PR. Minor
|
Adds typed, collection-local
initialDataandinitialDataUpdatedAtsupport to eager Query Collections. Initial Query responses now materialize immediately as collection rows while retaining TanStack Query's cache identity, freshness, and wrapped-response semantics.Root Cause
Query Collections already materialized data preloaded into a
QueryClient, but they could not declare initial data per collection. Applications sharing one client across many collections therefore had to choose between overly broad client defaults and imperative cache seeding. Forwarding defaults unchanged was also unsafe for on-demand subsets and placeholder data: neither has collection-wide row-membership semantics.Approach
initialDataandinitialDataUpdatedAtoptions using the original Query response type.select, materialization, and row-ownership pipeline.placeholderDatadefaults because placeholder results are observer-local UI state, not Query cache data suitable for normalized rows.Key Invariants
QueryClient.selectmaterializes rows without replacing the original wrapped response in the Query cache.Non-goals
placeholderDataas DB rows.Trade-offs
On-demand mode rejects explicit initial-data options rather than guessing subset membership. Callers that know an exact derived key can still seed or hydrate that Query cache entry. Eager mode suppresses placeholder defaults to keep observer presentation state out of the collection's shared normalized state.
Verification
The focused Query Collection suite covers fresh and stale initial data, function initializers, wrapped response projection, shared clients and exact keys, existing-cache precedence, on-demand rejection/default suppression, and placeholder-default suppression. Lint passes with existing warnings.
Files changed
packages/query-db-collection/src/query.ts— adds the public options and observer initialization policy.packages/query-db-collection/src/errors.ts— adds the on-demand configuration error.packages/query-db-collection/tests/query.test.ts— adds initialization and isolation coverage.docs/collections/query-collection.md— documents usage and user-visible semantics.docs/collections/query-initial-placeholder-data-design.md— records authority, ownership, transitions, writes, and persistence decisions..changeset/young-cats-initialize.md— records the minor package feature.Closes #346. Advances #1643.
Summary by CodeRabbit
New Features
initialDataandinitialDataUpdatedAt.Documentation